import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Creat by TanLei
 * Description:
 * User:86183
 * Date:2022-06-16
 * Time:13:51
 */
public class Test3 {
    //创建一个线程，通过这个线程计算1+2+3+。。。+1000
    //此处Callable定义出了一个带有返回值结果的任务
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 1; i < 1000; i++) {
                    sum += i;
                }
                return sum;
            }
            Thread t = new Thread();

        };

        //Thread的构造方法，没有直接传Collable的版本
        //需要另一个类辅助以下
        FutureTask<Integer> task = new FutureTask<>(callable);
        Thread t = new Thread(task);
        t.start();

        //FutureTask 是小票，就需要拿着这个小票来取餐
        Integer result = task.get();
        System.out.println(result);
    }
}
